from flask import Flask, request, render_template
from pygments.lexer import default

# 使用Flask类创建应用程序实例
app = Flask(__name__)
print("__name__", __name__)

# 注册路由
@app.route('/hellword')
def hello_word():
    return 'Hello World! Python'

@app.route('/blog/list')
def blog_list():
    return 'blog_list'

@app.route('/blog/<int:id>')
def blog_detail(id):
    '''
    url变量
    :param id:
    :return:
    '''
    return f'blog_detail{id}'

@app.route('/blog/list2')
def blog_list2():
    '''
    url带参数
    :param id:
    :return:
    '''
    page = request.args.get('page',default =1,type = int)
    size = request.args.get('size',default =10,type = int)
    print(page,size)
    return f'博客列表{page},{size}'

@app.route('/login',methods=['GET','POST'])
def login():
    '''
    登录
    :return:
    '''
    if request.method == 'GET':
        return render_template('login.html')
    else:
        username = request.form.get('username')
        password = request.form.get('password')
        return f'登录成功{username},{password}'

@app.route('/test_hook')
def test_hook():
    '''
    钩子函数
    :return:
    '''
    print('test_hook')
    return 'ok'

@app.before_request
def before_someing_request():
    '''
    请求前钩子
    :return:
    '''
    print('before_request')

if __name__ == '__main__':
    app.run()

